<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\response\Json;

/**
 * @mixin \think\Model
 */
class CheckListDetails extends Base
{

    protected $name = 'check_list_details';

    public function proTopic()
    {
        return $this->hasOne(ProTopic::class, 'id', 'topic_id')->field('id,title');
    }
    /**
     * 新建场所
     */
    public static function postPlace($param, $user)
    {
        parent::startTrans();
        try {
            $companydata['create_time'] = time();
            $companydata['is_create'] = 2;
            $company = Company::create($companydata);
            $check_list_id = CheckList::create(['company_id' => $company['id'],'is_create'=>2]);
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功', 'data' => ['check_list_id' => $check_list_id['id']]];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 提交数据
     */
    public static function postData($param, $user)
    {
        parent::startTrans();
        try {
            $param['data'] = json_decode(htmlspecialchars_decode($param['data']), true);
            // $list=json_decode($checkData,true);
            /***
             * 添加公司信息
             */
            if (isset($param['check_list_id']) && !empty($param['check_list_id'])) {
                $checkList = CheckList::where(['id' => $param['check_list_id'], 'is_del' => 0])->order('id desc')->find();
            }

            $infos = DistrictPro::where(['id' => $param['pro_id'], 'is_del' => 0])->find();
            if (empty($infos)) {
                return ['code' => -1, 'msg' => '项目区域不存在！'];
            }
            // if (empty($company)) {
            $companydata['company_name'] = $param['company_name'];
            $companydata['place_id'] = $param['pro_id'];
            $companydata['pro_id'] = $infos['pro_id'];
            $companydata['height'] = $param['height'];
            $companydata['address'] = $param['address'];
            $companydata['area'] = $param['area'];
            $companydata['fzname'] = $param['fzname'];
            $companydata['lxtel'] = $param['lxtel'];
            $companydata['images'] = $param['images'];
            $companydata['status'] = $param['status'];
            $companydata['note'] = isset($param['note']) ? $param['note'] : '';
            // $companydata['lat'] = $param['lat'];
            // $companydata['lng'] = $param['lng'];
            $companydata['district_id'] = $param['district_id'];
            $companydata['create_time'] = time();
            if (isset($param['check_list_id']) && !empty($param['check_list_id'])) {
                Company::where(['id' => $checkList['company_id']])->update($companydata);
                $company_id = $checkList['company_id'];
            } else {
                $companyid = Company::create($companydata);
                $company_id = $companyid->id;
            }
            // } else {
            //     $company_id = $company->id;
            // }
            /**
             * 添加检查记录
             */
            // $checkList = CheckList::where(['pro_id' => $param['pro_id'], 'company_id' => $company_id, 'is_del' => 0])->find();
            // if (empty($checkList)) {
            $checkListdata['company_id'] = $company_id;
            $checkListdata['pro_id'] = $infos['pro_id'];
            $checkListdata['district_id'] = $param['district_id'];
            $checkListdata['note'] = isset($param['note']) ? $param['note'] : '';
            $checkListdata['one_uid'] = $user->id;
            $checkListdata['one_time'] = time();
            $checkListdata['create_time'] = time();
            $checkListdata['types'] = 1;
            $checkListdata['one_status'] = 2;
            if (isset($param['check_list_id']) && !empty($param['check_list_id'])) {
                CheckList::where(['id' => $checkList['id']])->update($checkListdata);
                $check_list_id = $checkList->id;
            } else {
                $checkListid = CheckList::create($checkListdata);
                $check_list_id = $checkListid->id;
            }
            /**
             * 初检报告
             */
            $check_one = CheckReport::where(['check_list_id' => $check_list_id, 'types' => 1])->count();
            if ($check_one == 0) {
                CheckReport::create(['check_list_id' => $check_list_id, 'types' => 1]);
            }
            /**
             * 隐患报告
             */
            $check_danger = CheckReport::where(['check_list_id' => $check_list_id, 'types' => 4])->count();
            if ($check_danger == 0) {
                CheckReport::create(['check_list_id' => $check_list_id, 'types' => 4]);
            }
            $sumcount = 0;
            $points = 0;
            if ($param['status'] == 0) {
                foreach ($param['data'] as $k => $v) {
                    $sum = 0;
                    foreach ($v['child'] as $kk => $vv) {
                        // if(isset($vv['points'])){
                        //     if($vv['points']=='' || $vv['points']<=0){
                        //         return ['code' => -1, 'msg' => '扣分不能为空!'];
                        //     }
                        // }else{
                        //     return ['code' => -1, 'msg' => '请填写扣除分数!'];
                        // }

                        if (!empty($vv)) {
                            $details = parent::where(['topic_id' => $vv['topic_id'], 'check_list_id' => $check_list_id, 'types' => 1, 'district_id' => $param['district_id'], 'company_id' => $company_id, 'is_del' => 0])->find();
                            // if (!empty($details)) {
                            //     parent::where(['id'=>$details['id']])->delete();
                            // }
                            //$detailsdata['desc'] = $vv['desc'];
                            if (isset($vv['desc']) && !empty($vv['desc'])) {
                                $detailsdata['desc'] = $vv['desc'];
                            } else {
                                $detailsdata['desc'] = '';
                            }
                            if (isset($vv['images']) && !empty($vv['images'])) {
                                $detailsdata['images'] = $vv['images'];
                            } else {
                                $detailsdata['images'] = '';
                            }
                            $ProTopic = ProTopic::where(['id' => $vv['topic_id'], 'is_del' => 0])->find();
                            if (empty($vv['points'])) {
                                $vpoints = $ProTopic['score'];
                            } else {
                                $vpoints = $ProTopic['score'] + $vv['points'];
                            }
                            $detailsdata['points'] = $vpoints;
                            $detailsdata['topic_id'] = $vv['topic_id'];
                            $detailsdata['types'] = 1;
                            $detailsdata['check_list_id'] = $check_list_id;
                            $detailsdata['company_id'] = $company_id;
                            $detailsdata['district_id'] = $param['district_id'];
                            $detailsdata['cate_id'] = $v['cate_id'];
                            $detailsdata['create_time'] = time();
                            //$detail = parent::where(['topic_id' => $vv['topic_id'], 'check_list_id' => $check_list_id, 'types' => 1, 'district_id' => $param['district_id'], 'company_id' => $company_id, 'is_del' => 0])->find();
                            if (empty($details)) {
                                $detailsAll[] = $detailsdata;
                            }
                            $sum += $vpoints;
                        }
                    }
                    $points += self::postDataSum($v['cate_id'], $sum);
                    $sumcount += $sum;
                }
                //最大分数
                $maxnum = Pro::where(['id' => $infos['pro_id'], 'is_del' => 0])->value('num');
                // if ($sumcount > $maxnum) {
                //     return ['code' => -1, 'msg' => '您所扣分数超出'.$maxnum.'分啦!'];
                // }
                $one_scoredf = $maxnum - $points;
                if ($one_scoredf > 0) {
                    $one_score = $one_scoredf;
                } else {
                    $one_score = 0;
                }

                CheckList::where(['id' => $check_list_id, 'is_del' => 0])->update(['one_score' => $one_score]);


                // if(!empty($detailsSave)){
                //     foreach($detailsSave as $ks=>$vs){
                //         // $detailsdatas['desc'] = $vs['desc'];
                //         // $detailsdatas['images'] = $vs['images'];
                //         // $detailsdatas['points'] = $vs['points'];
                //         // $detailsdatas['topic_id'] = $vs['topic_id'];
                //         // $detailsdatas['check_list_id'] = $vs['check_list_id'];
                //         // $detailsdatas['company_id'] = $vs['company_id'];
                //         // $detailsdatas['district_id'] = $vs['district_id'];
                //         // $detailsdatas['cate_id'] = $vs['cate_id'];
                //         parent::where(['id'=>$vs['id']])->update($vs);
                //     }
                // }
                if (!empty($detailsAll)) {
                    parent::insertAll($detailsAll);
                }
                //print('<pre>');print_r($detailsSave.$detailsAll);die;
                $dangerCount = parent::where(['district_id' => $param['district_id'], 'company_id' => $company_id, 'types' => 1, 'check_list_id' => $check_list_id, 'is_del' => 0])->count();
            } else {
                $one_score = 100;
                $dangerCount = 0;
                CheckList::where(['id' => $check_list_id, 'is_del' => 0])->update(['one_score' => $one_score]);
            }
            Company::where(['id' => $company_id])->update(['score' => $one_score]);
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功', 'data' => ['one_score' => $one_score, 'dangerCount' => $dangerCount, 'check_list_id' => $check_list_id]];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 提交数据
     */
    public static function postCheckData($param, $user)
    {
        parent::startTrans();
        try {
            $param['data'] = json_decode(htmlspecialchars_decode($param['data']), true);
            /**
             * 获取检查记录
             */
            $checkList = CheckList::where(['id' => $param['check_list_id'], 'is_del' => 0])->find();
            //print('<pre>');print_r($param);die;
            $sumcount = 0;
            $points = 0;
            if ($param['types'] == 1) {
                $types = 2;
            } else {
                $types = 3;
            }
            // $types=$checkList['types'];

            foreach ($param['data'] as $k => $v) {
                $sum = 0;
                foreach ($v['child'] as $kk => $vv) {
                    if (!empty($vv)) {
                        $details = parent::where(['topic_id' => $vv['topic_id'], 'check_list_id' => $param['check_list_id'], 'types' => $types, 'district_id' => $checkList['district_id'], 'company_id' => $checkList['company_id'], 'is_del' => 0])->find();
                        if (isset($vv['desc']) && !empty($vv['desc'])) {
                            $detailsdata['desc'] = $vv['desc'];
                        } else {
                            $detailsdata['desc'] = '';
                        }
                        if (isset($vv['images']) && !empty($vv['images'])) {
                            $detailsdata['images'] = $vv['images'];
                        } else {
                            $detailsdata['images'] = '';
                        }
                        $ProTopic = ProTopic::where(['id' => $vv['topic_id'], 'is_del' => 0])->find();
                        if (empty($vv['points'])) {
                            $vpoints = $ProTopic['score'];
                        } else {
                            $vpoints = $ProTopic['score'] + $vv['points'];
                        }
                        $detailsdata['topic_id'] = $vv['topic_id'];
                        $detailsdata['types'] = $types;
                        $detailsdata['check_list_id'] = $param['check_list_id'];
                        $detailsdata['company_id'] = $checkList['company_id'];
                        $detailsdata['district_id'] = $checkList['district_id'];
                        $detailsdata['cate_id'] = $v['cate_id'];
                        $detailsdata['create_time'] = time();
                        if (!empty($details)) {
                            $detailsAll[] = $detailsdata;
                        }
                        $sum += $vpoints;
                    }
                }
                $points += self::postDataSum($v['cate_id'], $sum);
                $sumcount += $sum;
            }
            if ($sumcount > 100) {
                return ['code' => -1, 'msg' => '您所扣分数超出100分啦!'];
            }
            $one_score = 100 - $points;
            if ($types == 2) {
                $scoredata['two_score'] = $one_score;
                $scoredata['two_time'] = time();
                // $scoredata['two_company_sign']=$param['one_company_sign'];
                // $scoredata['two_uid_sign']=$param['one_uid_sign'];
                $scoredata['two_status'] = 2;
            } else {
                $scoredata['three_score'] = $one_score;
                $scoredata['three_time'] = time();
                // $scoredata['three_company_sign']=$param['one_company_sign'];
                // $scoredata['three_uid_sign']=$param['one_uid_sign'];
                $scoredata['three_status'] = 2;
            }
            CheckList::where(['id' => $param['check_list_id'], 'is_del' => 0])->update($scoredata);
            if (!empty($detailsAll)) {
                parent::insertAll($detailsAll);
            }

            /**
             * 二三检报告
             */
            $check_one = CheckReport::where(['check_list_id' => $param['check_list_id'], 'types' => $types])->count();
            if ($check_one == 0) {
                CheckReport::create(['check_list_id' => $param['check_list_id'], 'types' => $types]);
            }
            $dangerCount = parent::where(['district_id' => $checkList['district_id'], 'company_id' => $checkList['company_id'], 'check_list_id' => $checkList['id'], 'types' => $types, 'is_del' => 0])->count();
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功', 'data' => ['one_score' => $one_score, 'dangerCount' => $dangerCount, 'check_list_id' => $param['check_list_id']]];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 累计分类计算分数是否超过分类设置的分数
     */
    public static function postDataSum($ids, $sum)
    {
        $res = ProCate::where(['id' => $ids, 'is_del' => 0])->find();
        if ($sum > $res['score']) {
            return $res['score'];
        } else {
            return $sum;
        }
    }

    /**
     * 上传隐患图片
     */
    public static function postReform($param, $user)
    {
        parent::startTrans();
        try {

            $data = json_decode(htmlspecialchars_decode($param['data']), true);

            if (count($data) != 0) {
                foreach ($data as $k => $v) {
                    $find = parent::where(['id' => $v['id'], 'is_del' => 0])->find();
                    $check_report = CheckReport::where(['types' => 5, 'check_list_id' => $find['check_list_id']])->find();
                    if (empty($check_report) && !empty($find)) {
                        $datainfo['check_list_id'] = $find['check_list_id'];
                        $datainfo['types'] = 5;
                        CheckReport::create($datainfo);
                    }
                    if (!empty($v['picture']) && !empty($find)) {
                        $find->types = 2;
                        $find->picture = $v['picture'];
                        $find->save();
                    }
                }
                parent::commit();
                return ['code' => 1, 'msg' => '整改成功！'];
            } else {
                return ['code' => -1, 'msg' => '整改数据不能为空'];
            }
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
